﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using System;
using System.Collections.Generic;
using System.IO;

namespace Blueprints
{
    /// <summary>
    /// Allows quick access to the serialization methods of a BlueprintContext.
    /// </summary>
    public static class Blueprint
    {
        /// <summary>
        /// Deserializes a blueprint.
        /// </summary>
        /// <typeparam name="TargetType">The type of object to deserialize.</typeparam>
        /// <param name="uri">The uri at which the blueprint is located.</param>
        /// <param name="mediaType">The media type with which the blueprint is stored.</param>
        /// <returns>The deserialized object.</returns>
        public static TargetType Deserialize<TargetType>(Uri uri, MediaType mediaType = MediaType.Unknown)
        {
            if (uri == null) throw new ArgumentNullException(nameof(uri));
            using (BlueprintContext context = new BlueprintContext(typeof(TargetType)))
            {
                return context.Deserialize<TargetType>(uri, mediaType);
            }
        }

        /// <summary>
        /// Deserializes a blueprint.
        /// </summary>
        /// <typeparam name="TargetType">The type of object to deserialize.</typeparam>
        /// <param name="stream">The stream from which to read.</param>
        /// <param name="uri">The uri at which the blueprint is located. This is used to resolve relative links within the blueprint.</param>
        /// <param name="mediaType">The media type with which the blueprint is stored.</param>
        /// <returns>The deserialized object.</returns>
        public static TargetType Deserialize<TargetType>(Stream stream, Uri uri = null, MediaType mediaType = MediaType.Unknown)
        {
            if (stream == null) throw new ArgumentNullException(nameof(stream));
            if (!stream.CanRead) throw new ArgumentException("The provided stream must be readable", nameof(stream));
            using (BlueprintContext context = new BlueprintContext(typeof(TargetType)))
            {
                return context.Deserialize<TargetType>(stream, uri, mediaType);
            }
        }

        /// <summary>
        /// Deserializes a blueprint, and assigns its values to <paramref name="instance"/>.
        /// </summary>
        /// <typeparam name="TargetType">The type of object to deserialize properties for.</typeparam>
        /// <param name="instance">The object instance to assign the values to.</param>
        /// <param name="uri">The uri at which the blueprint is located.</param>
        /// <param name="mediaType">The media type with which the blueprint is stored.</param>
        public static void DeserializeProperties<TargetType>(TargetType instance, Uri uri, MediaType mediaType = MediaType.Unknown)
        {
            if (instance == null) throw new ArgumentNullException(nameof(instance));
            if (uri == null) throw new ArgumentNullException(nameof(uri));
            using (BlueprintContext context = new BlueprintContext(typeof(TargetType)))
            {
                context.DeserializeProperties(instance, uri, mediaType);
            }
        }

        /// <summary>
        /// Deserializes a blueprint, and assigns its values to <paramref name="instance"/>.
        /// </summary>
        /// <typeparam name="TargetType">The type of object to deserialize properties for.</typeparam>
        /// <param name="instance">The object instance to assign the values to.</param>
        /// <param name="stream">The stream from which to read.</param>
        /// <param name="uri">The uri at which the blueprint is located. This is used to resolve relative links within the blueprint.</param>
        /// <param name="mediaType">The media type with which the blueprint is stored.</param>
        public static void DeserializeProperties<TargetType>(TargetType instance, Stream stream, Uri uri = null, MediaType mediaType = MediaType.Unknown)
        {
            if (instance == null) throw new ArgumentNullException(nameof(instance));
            if (stream == null) throw new ArgumentNullException(nameof(stream));
            if (!stream.CanRead) throw new ArgumentException("The provided stream must be readable", nameof(stream));
            using (BlueprintContext context = new BlueprintContext(typeof(TargetType)))
            {
                context.DeserializeProperties(instance, stream, uri, mediaType);
            }
        }

        /// <summary>
        /// Deserializes items from a blueprint.
        /// </summary>
        /// <typeparam name="ItemType">The type of items to deserialize.</typeparam>
        /// <param name="uri">The uri at which the blueprint is located.</param>
        /// <param name="mediaType">The media type with which the blueprint is stored.</param>
        /// <returns>The deserialized items.</returns>
        public static IEnumerable<ItemType> DeserializeItems<ItemType>(Uri uri, MediaType mediaType = MediaType.Unknown)
        {
            if (uri == null) throw new ArgumentNullException(nameof(uri));
            using (BlueprintContext context = new BlueprintContext(typeof(ItemType)))
            {
                foreach (ItemType item in context.DeserializeItems<ItemType>(uri, mediaType))
                {
                    yield return item;
                }
            }
        }

        /// <summary>
        /// Deserializes multiple items of the same type from a blueprint.
        /// </summary>
        /// <typeparam name="ItemType">The type of items to deserialize.</typeparam>
        /// <param name="stream">The stream from which to read.</param>
        /// <param name="uri">The uri at which the blueprint is located. This is used to resolve relative links within the blueprint.</param>
        /// <param name="mediaType">The media type with which the blueprint is stored.</param>
        /// <returns>The deserialized items.</returns>
        public static IEnumerable<ItemType> DeserializeItems<ItemType>(Stream stream, Uri uri = null, MediaType mediaType = MediaType.Unknown)
        {
            if (stream == null) throw new ArgumentNullException(nameof(stream));
            if (!stream.CanRead) throw new ArgumentException("The provided stream must be readable", nameof(stream));
            using (BlueprintContext context = new BlueprintContext(typeof(ItemType)))
            {
                foreach (ItemType item in context.DeserializeItems<ItemType>(stream, uri, mediaType))
                {
                    yield return item;
                }
            }
        }

        /// <summary>
        /// Serializes an object.
        /// </summary>
        /// <typeparam name="TargetType">The type of object to serialize.</typeparam>
        /// <param name="stream">The stream to which to write.</param>
        /// <param name="instance">The object to serialize.</param>
        /// <param name="uri">The uri at which the blueprint is to be stored. This is used to generate relative links within the blueprint.</param>
        /// <param name="mediaType">The media type with which to store the blueprint.</param>
        /// <param name="rootName">The name of the root node.</param>
        public static void Serialize<TargetType>(Stream stream, TargetType instance, Uri uri = null, MediaType mediaType = MediaType.Unknown, string rootName = null)
        {
            if (stream == null) throw new ArgumentNullException(nameof(stream));
            if (instance == null) throw new ArgumentNullException(nameof(instance));
            if (!stream.CanWrite) throw new ArgumentException("The provided stream must be writeable", nameof(stream));
            using (BlueprintContext context = new BlueprintContext(typeof(TargetType)))
            {
                context.Serialize(stream, instance, uri, mediaType, rootName);
            }
        }

        /// <summary>
        /// Serializes the properties of an object.
        /// </summary>
        /// <typeparam name="TargetType">The type of object to serialize.</typeparam>
        /// <param name="stream">The stream to which to write.</param>
        /// <param name="instance">The object of which to serialize properties.</param>
        /// <param name="uri">The uri at which the blueprint is to be stored. This is used to generate relative links within the blueprint.</param>
        /// <param name="mediaType">The media type with which to store the blueprint.</param>
        /// <param name="rootName">The name of the root node.</param>
        public static void SerializeProperties<TargetType>(Stream stream, TargetType instance, Uri uri = null, MediaType mediaType = MediaType.Unknown, string rootName = null)
        {
            if (stream == null) throw new ArgumentNullException(nameof(stream));
            if (instance == null) throw new ArgumentNullException(nameof(instance));
            if (!stream.CanWrite) throw new ArgumentException("The provided stream must be writeable", nameof(stream));
            using (BlueprintContext context = new BlueprintContext(typeof(TargetType)))
            {
                context.SerializeProperties(stream, instance, uri, mediaType, rootName);
            }
        }

        /// <summary>
        /// Serializes a multiple items of the same type.
        /// </summary>
        /// <typeparam name="ItemType">The type of items to serialize.</typeparam>
        /// <param name="stream">The stream to which to write.</param>
        /// <param name="items">The items to serialize.</param>
        /// <param name="uri">The uri at which the blueprint is to be stored. This is used to generate relative links within the blueprint.</param>
        /// <param name="mediaType">The media type with which to store the blueprint.</param>
        /// <param name="rootName">The name of the root node.</param>
        public static void SerializeItems<ItemType>(Stream stream, IEnumerable<ItemType> items, Uri uri = null, MediaType mediaType = MediaType.Unknown, string rootName = null)
        {
            if (stream == null) throw new ArgumentNullException(nameof(stream));
            if (items == null) throw new ArgumentNullException(nameof(items));
            if (!stream.CanWrite) throw new ArgumentException("The provided stream must be writeable", nameof(stream));
            using (BlueprintContext context = new BlueprintContext(typeof(ItemType)))
            {
                context.SerializeItems(stream, items, uri, mediaType, rootName);
            }
        }
    }
}